<?php
/**
 * Created by PhpStorm.
 * User: zws
 * Date: 2018/9/8
 * Time: 11:02
 * 检测人员管理
 */
namespace app\admin\service;

use app\common\model\UserModel;
use think\Exception;
use think\facade\Log;

class UserService
{
    protected static $userModel;

    /**
     * UserService constructor.
     * @param UserModel $userModel
     * @author zws
     */
    public function __construct(UserModel $userModel)
    {
        self::$userModel = $userModel;
    }

    /**
     * 检测用户列表
     * @param $param
     * @return array|\PDOStatement|string|\think\Collection
     * @author zws
     */
    public function userList($param)
    {
        if(!isset($param['page']) || empty($param['page'])){
            $param['page'] = 1;
        }
        if(!isset($param['limit']) || empty($param['limit'])){
            $param['limit'] = 10;
        }
        $where = ['is_del' => 1];
        // 获取总数
        $count = self::$userModel->where($where)->count();
        if ($count < 1) {
            return ['data' => [], 'count' => $count];
        }
        // 查询检测人员列表
        $result = self::$userModel->where($where)
            ->page($param['page'], $param['limit'])
            ->order('create_time', 'desc')
            ->select();
        return ['data' => $result, 'count' => $count];
    }

    /**
     * 获取单个用户数据
     * @param $param
     * @return array|null|\PDOStatement|string|\think\Model
     * @author zws
     */
    public function editUser($param){
        if(empty($param) || !isset($param['id']) || empty($param['id'])){
            app_fail(9997);//参数错误
        }
        $row = self::$userModel->where('id', '=', $param['id'])->find();
        if(!$row){
            app_fail(7999);
        }
        return $row;

    }

    /**
     * 检测用户添加、修改
     * @param $param
     * @return bool
     * @author zws
     */
    public function update($param)
    {
        if(empty($param))
        {
            app_fail(9997);//参数错误
        }
        $user = self::$userModel;
        if(isset($param['id']) && !empty($param['id'])){
            $data['id'] = $param['id'];
        }
        if (!empty($param['username'])) {
            $data['user_name'] = $param['username'];
        }

        if (!empty($param['phone'])) {
            $data['phone'] = $param['phone'];
        }
        if (!empty($param['password'])) {
            $data['password'] = md5($param['password']);
        }
        if (!empty($param['work_num'])) {
            $data['work_num'] = $param['work_num'];
        }
        if (!empty($param['company'])) {
            $data['company'] = $param['company'];
        }
        if (!empty($param['image'])) {
            $data['image'] = '';
        }
        if (!empty($param['status'])) {
            $data['status'] = $param['status'] == 1 ? 2 : 1;
        }
        if (!empty($param['is_del'])) {
            $data['is_del'] = isset($param['is_del']) ? $param['is_del'] : 1;
        }


        try{
            $where = empty($data['id']) ? [] : ['id' => $data['id']];
            $result = $user->save($data, $where);
            return $result;
        }catch (\Exception $e){
            app_fail(9998);//用户编辑失败
        }
    }
}